<?php
require_once('config.php');
require_once('../congo-sso/OauthClient.php');

$uri = trim($_SERVER['REQUEST_URI'], '/');

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: *");
header('Access-Control-Allow-Headers: *');

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    exit();
}

if (strpos($uri, 'oauth/wx/code2token') === 0) {
    // 应用的appKey，应用的密钥,由开放平台发放
    $oauthClient = new CongoSSO\OAuthClient($APP_KEY, $SECRET_KEY);
    // 应用客户端部署地址
    $oauthClient->setSSOClientServerUrl($SSO_CLIENT_SERVER);

    // Get token
    $result = $oauthClient->getTokenByTicket($_GET['code']);
    error_log("return: " . json_encode($result), 0);
    header('Content-Type:application/json; charset=utf-8');

    exit(json_encode($result));
} else {
    exit('success');
}
?>
